package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ScrollPane;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import domain.Player;
import domain.models.MyListModel;
import domain.models.RegisteredPlayersTableModel;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import javax.swing.ButtonGroup;
import javax.swing.UIManager;

public class ConfigurationTournamentPanel extends JPanel{

	private JTable registeredPlayersTbl;
	private JList playersToBePairedLstbx;
	private JButton moveSelectionToRightBtn;
	private JButton moveAllToRightBtn;
	private JButton moveSelectionToLeftBtn;
	private JButton moveAllToLeftBtn;
	private JButton addPlayersBtn;
	
	private JScrollPane playersToBePairedScrollPane;
	
	private Frame frame;
	private GuiController guiController;
	private RegisteredPlayersTableModel registeredPlayersTableModel;
	private MyListModel playersToBePairedListModel;
	
	private JPanel buttonPanel;
	private JScrollPane registeredPlayersJScrollPane;
	private MyActionListener  myActionListener;
	private JLabel tournamentNameLbl;
	private JButton pairUpBtn;
	private JButton saveBtn;
	
	public ConfigurationTournamentPanel(Frame frame)
	{
		super();
		this.frame = frame;
		this.guiController = frame.getGuiController();
		this.registeredPlayersTableModel = guiController.getDomainController().getRegisteredPlayersTableModel();
		this.playersToBePairedListModel = guiController.getDomainController().getMyListModel();
		this.setBorder(new TitledBorder("ConfigurationTournamentPanel"));
		this.myActionListener = new MyActionListener();
		
		initGui();
	}

	private void initGui() {
		makeRegisteredPlayersTbl();
		makePlayersToBePairedLstbx();
		makeButtons();
		makeLbls();
		layoutComponents();
		initActionListeners();
	}
	
	private void makeRegisteredPlayersTbl()
	{

		registeredPlayersTbl = new JTable(registeredPlayersTableModel);
		registeredPlayersTbl.setVisible(true);
		registeredPlayersTbl.setShowGrid(true);
		registeredPlayersTbl.setCellEditor(new DefaultCellEditor(new JTextField()));
		
		registeredPlayersTbl.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		
		registeredPlayersJScrollPane = new JScrollPane(registeredPlayersTbl);
		registeredPlayersJScrollPane.setBorder(new TitledBorder("Geregistreerde spelers"));
		
		
		
		
	}
	
	private void makePlayersToBePairedLstbx()
	{
		playersToBePairedLstbx = new JList(playersToBePairedListModel);
		playersToBePairedLstbx.setLayoutOrientation(JList.VERTICAL_WRAP);
		
	
		playersToBePairedLstbx.setBorder(new TitledBorder("Spelers klaar om te spelen"));
		playersToBePairedLstbx.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		
		playersToBePairedScrollPane = new JScrollPane(playersToBePairedLstbx);
		
	}
	
	private void makeLbls()
	{
		tournamentNameLbl = new JLabel();
		tournamentNameLbl.setForeground(Color.ORANGE);
		tournamentNameLbl.setText(guiController.getDomainController().getCurrentTournament().getName());
		
	}
	
	private void makeButtons()
	{
		buttonPanel = new JPanel();
		buttonPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Verplaats", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		moveSelectionToRightBtn = new JButton(">");
		moveSelectionToRightBtn.setToolTipText("Verplaats selectie naar rechts");

		moveAllToRightBtn = new JButton(">>");
		moveAllToRightBtn.setToolTipText("Verplaats alles naar rechts");

		moveSelectionToLeftBtn = new JButton("<");
		moveSelectionToLeftBtn.setToolTipText("Verplaats selectie naar links");
		moveSelectionToLeftBtn.addActionListener(myActionListener);

		moveAllToLeftBtn = new JButton("<<");
		moveAllToLeftBtn.setToolTipText("Verplaats alles naar rechts");
		
		addPlayersBtn = new JButton("Deelnemers toevoegen...");
		
		pairUpBtn = new JButton("Paring maken");
		
		saveBtn = new JButton("SAVE GAME");
		
		

	}
	
	private void layoutComponents()
	{
		buttonPanel.setLayout(new MigLayout("wrap 1"));		
		buttonPanel.add(moveSelectionToRightBtn);
		buttonPanel.add(moveAllToRightBtn);
		buttonPanel.add(moveSelectionToLeftBtn);
		buttonPanel.add(moveAllToLeftBtn);
		
		setLayout(new MigLayout());
		add(tournamentNameLbl, "span 2, wrap");
		add(registeredPlayersJScrollPane,"width 500, height 500");
		
		add(buttonPanel, "width 200, height 300");
		add(playersToBePairedScrollPane, "width 500, height 500, wrap");
		add(addPlayersBtn);
		add(pairUpBtn, "cell 2 2");
		add(saveBtn);
	
	}
	
	public void initActionListeners()
	{
		addPlayersBtn.addActionListener(myActionListener);
		moveSelectionToRightBtn.addActionListener(myActionListener);
		moveAllToRightBtn.addActionListener(myActionListener);
		moveAllToLeftBtn.addActionListener(myActionListener);
		pairUpBtn.addActionListener(myActionListener);
		saveBtn.addActionListener(myActionListener);
		

	}
	
	
	class MyActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent src) {
			if(src.getSource() == addPlayersBtn)
			{
				AddPlayersDialog d = new AddPlayersDialog(frame);
				
				d.setLocationRelativeTo(frame);
				d.setVisible(true);
			}
			else if(src.getSource() == moveSelectionToRightBtn)
			{
				 
				int[] selectedRows = registeredPlayersTbl.getSelectedRows();
				
				String player = null;
				if(selectedRows !=null)
				{
					for(int counter=0;counter<selectedRows.length;counter++)
					{
						player = registeredPlayersTableModel.getRow(selectedRows[counter]);
						guiController.getDomainController().addPlayersToBePaired(player);
					}		
				}		
			}
			else if(src.getSource() == moveAllToRightBtn)
			{
				
				String player = null;
				for (int counter = registeredPlayersTableModel.getRowCount() - 1; counter >= 0; counter--) {
					player = registeredPlayersTableModel.getRow(counter);
					guiController.getDomainController().addPlayersToBePaired(player);
				}
				
			}
			else if(src.getSource() == moveSelectionToLeftBtn)
			{
				int[] selectedRows = playersToBePairedLstbx.getSelectedIndices();
				
				String player = null;
				if(selectedRows !=null)
				{
					for(int counter=0;counter<selectedRows.length;counter++)
					{
						player = playersToBePairedListModel.getElementAt(selectedRows[counter]);
						guiController.getDomainController().removePlayerToBePaired(player);
					}		
				}		
			}
			else if(src.getSource() == moveAllToLeftBtn)
			{
				
				String player = null;
				for (int counter = playersToBePairedListModel.getSize() - 1; counter >= 0; counter--) {
					player = playersToBePairedListModel.getElementAt(counter);
					guiController.getDomainController().removePlayerToBePaired(player);
				}
				
			}
			else if(src.getSource() == pairUpBtn)
			{
				//TODO CHECK Parameter hoe meegeven? String[] of String[][]? OBSOLETE?
				guiController.getDomainController().pairUp();
			}
			else if(src.getSource() == saveBtn)
			{
				guiController.getDomainController().getPersistenceController().saveTournament(guiController.getDomainController().getCurrentTournament());
			}
			
		}
		
	}
	

}